package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.TicketRule;
import com.bestcem.xm.ticket.entity.mysql.TicketRuleDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 16:32
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketRuleConverter {

    public static TicketRule doToDto(TicketRuleDO ticketRuleDO) {
        if (Objects.isNull(ticketRuleDO)) {
            return null;
        }

        TicketRule ticketRule = new TicketRule();
        ticketRule.setId(ticketRuleDO.getTicketRuleId());
        ticketRule.setSurveyId(ticketRuleDO.getSurveyId());
        ticketRule.setTicketConfigId(ticketRuleDO.getTicketConfigId());
        ticketRule.setTitle(ticketRuleDO.getTitle());
        ticketRule.setGid(ticketRuleDO.getGid());
        ticketRule.setOperation(ticketRuleDO.getOperation());
        ticketRule.setCreator(ticketRuleDO.getCreator());
        ticketRule.setVersion(ticketRuleDO.getVersion());
        ticketRule.setType(ticketRuleDO.getType());
        ticketRule.setCondition(CommonConverter
                .stringToObject(ticketRuleDO.getCondition(), TicketRule.Condition.class));
        ticketRule.setTargetList(CommonConverter
                .stringToList(ticketRuleDO.getTargetList(), TicketRule.Target.class));
        ticketRule.setConditionList(CommonConverter
                .stringToList(ticketRuleDO.getConditionList(), TicketRule.Condition.class));
        ticketRule.setSample(ticketRuleDO.getSample());

        CommonConverter.setEntity(ticketRule, ticketRuleDO);
        return ticketRule;
    }

    public static List<TicketRule> doListToDtoList(List<TicketRuleDO> ticketRuleDOList) {
        List<TicketRule> list = new ArrayList<>();
        if (Objects.isNull(ticketRuleDOList)) {
            return list;
        }

        for (TicketRuleDO ticketRuleDO : ticketRuleDOList) {
            TicketRule ticketRule = doToDto(ticketRuleDO);
            list.add(ticketRule);
        }
        return list;
    }

    @Nullable
    public static TicketRuleDO dtoToDo(TicketRule ticketRule) {
        if (Objects.isNull(ticketRule)) {
            return null;
        }

        TicketRuleDO ticketRuleDO = new TicketRuleDO();
        ticketRuleDO.setTicketRuleId(ticketRule.getId());
        ticketRuleDO.setSurveyId(ticketRule.getSurveyId());
        ticketRuleDO.setTicketConfigId(ticketRule.getTicketConfigId());
        ticketRuleDO.setTitle(ticketRule.getTitle());
        ticketRuleDO.setGid(ticketRule.getGid());
        ticketRuleDO.setOperation(ticketRule.getOperation());
        ticketRuleDO.setCreator(ticketRule.getCreator());
        ticketRuleDO.setVersion(ticketRule.getVersion());
        ticketRuleDO.setType(ticketRule.getType());
        ticketRuleDO.setCondition(CommonConverter.objectToString(ticketRule.getCondition()));
        ticketRuleDO.setTargetList(CommonConverter.listToString(ticketRule.getTargetList()));
        ticketRuleDO.setConditionList(CommonConverter.listToString(ticketRule.getConditionList()));
        ticketRuleDO.setSample(ticketRule.getSample());

        CommonConverter.setDo(ticketRule, ticketRuleDO);
        return ticketRuleDO;
    }
}
